//
//  LGAccountInputView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/13.
//

#import "LGAccountInputView.h"

@interface LGAccountInputView()
/**   */
@property (nonatomic , assign)NSInteger index;

@end

@implementation LGAccountInputView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        self.layer.borderWidth = 1;
        self.layer.cornerRadius = 10;
        [self addSubview:self.contenTF];
        [self addSubview:self.actionBtn];
        [self.contenTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(viewPix(16));
            make.right.equalTo(self.actionBtn.mas_left).offset(-viewPix(16));
            make.top.bottom.equalTo(self);
        }];
        [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self);
            make.width.equalTo(@(viewPix(56)));
        }];
    }
    return self;
}

-(void)setHasBorder:(BOOL)hasBorder{
    _hasBorder = hasBorder;
    if(hasBorder == NO){
        self.layer.borderColor = [UIColor clearColor].CGColor;
        self.layer.borderWidth = 0;
    }else{
        self.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        self.layer.borderWidth = 1;
    }
}


-(void)setType:(NSString *)type{
    _type = type;
    NSString *placeHolder = @"";
    if([type isEqualToString:@"phone"]){
        //手机号
        placeHolder = @"+86   请输入您的手机号";
        self.contenTF.keyboardType = UIKeyboardTypePhonePad;
    
        self.actionBtn.hidden = YES;
        [self.actionBtn setImage:[UIImage imageNamed:@"clearPhone"] forState:UIControlStateNormal];
        [self.actionBtn setImage:[UIImage imageNamed:@"clearPhone"] forState:UIControlStateHighlighted];
        [self.actionBtn addTarget:self action:@selector(clearContentAction) forControlEvents:UIControlEventTouchUpInside];
        
    }else if ([type isEqualToString:@"code"]){
        //验证码
        placeHolder = @"请输入短信验证码";
        self.contenTF.keyboardType = UIKeyboardTypeNumberPad;
        
        [self.actionBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.actionBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, viewPix(16))];
        self.actionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.actionBtn setTitleColor:[UIColor colorWithString:@"#2E8FFF"] forState:UIControlStateNormal];
        [self.actionBtn setTitleColor:[UIColor colorWithString:@"#999999"] forState:UIControlStateSelected];
        [self.actionBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(viewPix(150)));
        }];
        
    }else if ([type isEqualToString:@"password"]){
        //密码
        self.contenTF.secureTextEntry = YES;
        placeHolder = @"请输入您的密码";
        self.contenTF.keyboardType = UIKeyboardTypeDefault;
        
        [self.actionBtn setImage:[UIImage imageNamed:@"hidenPassword"] forState:UIControlStateNormal];
        [self.actionBtn setImage:[UIImage imageNamed:@"showPassword"] forState:UIControlStateSelected];
        [self.actionBtn addTarget:self action:@selector(passwordShowAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    self.contenTF.attributedPlaceholder = [placeHolder attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]}];
}

//手机号-->清空输入内容
-(void)clearContentAction{
    self.contenTF.text = @"";
}

//密码-->是否显示输入内容
-(void)passwordShowAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.contenTF.secureTextEntry = !sender.selected;
}

//手机号-->内容显示
-(void)textFieldEditingChanged:(UITextField *)textField{
    if([self.type isEqualToString:@"phone"]){
        //手机号
        self.actionBtn.hidden = (textField.text.length == 0)?YES:NO;
        //输入手机号
        if (textField.text.length > self.index) {
            if (textField.text.length == 4 || textField.text.length == 9 ) {
                //输入
                NSMutableString * str = [[NSMutableString alloc ] initWithString:textField.text];
                [str insertString:@" " atIndex:(textField.text.length-1)];
                textField.text = str;
            }
            if (textField.text.length >= 13 ) {
                //输入完成
                textField.text = [textField.text substringToIndex:13];
            }

        }else if (textField.text.length < self.index){
            //删除
            if (textField.text.length == 4 || textField.text.length == 9) {
                textField.text = [NSString stringWithFormat:@"%@",textField.text];
                textField.text = [textField.text substringToIndex:(textField.text.length-1)];
            }
        }
        self.index = textField.text.length;
    }else{
        //去除空格
        textField.text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}

-(UITextField *)contenTF{
    if(!_contenTF){
        _contenTF = [[UITextField alloc]init];
        _contenTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contenTF.textColor = [UIColor colorWithString:@"#333333"];
        _contenTF.textAlignment = NSTextAlignmentLeft;
        _contenTF.font = LGFont(16);
        [_contenTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _contenTF;
}

-(UIButton *)actionBtn{
    if(!_actionBtn){
        _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _actionBtn.titleLabel.font = LGFontWeight(14, UIFontWeightMedium);
    }
    return _actionBtn;
}



@end
